package cn.bdqn.applysystem.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bdqn.applysystem.entity.Grade;
import cn.bdqn.applysystem.entity.Notice;
import cn.bdqn.applysystem.entity.Student;
import cn.bdqn.applysystem.entity.User;
import cn.bdqn.applysystem.service.GradeService;
import cn.bdqn.applysystem.service.NoticeService;
import cn.bdqn.applysystem.service.StudentService;
import cn.bdqn.applysystem.service.SubjectService;
import cn.bdqn.applysystem.service.UserService;

import com.alibaba.fastjson.JSONArray;

@Controller("studentController")
@RequestMapping("/sys/student")
public class StudentController {
	
	@Autowired
	@Qualifier("studentServiceImpl")
	private StudentService studentService;
	
	@Autowired
	@Qualifier("subjectServiceImpl")
	private SubjectService subjectService;
	
	@Autowired
	@Qualifier("userServiceImpl")
	private UserService userService;
	
	@Autowired
	@Qualifier("noticeServiceImpl")
	private NoticeService noticeService;
	
	@Autowired
	@Qualifier("gradeServiceImpl")
	private GradeService gradeService;
	
	@Autowired
	@Qualifier("notice")
	private Notice notice;
	
	@Autowired
	@Qualifier("grade")
	private Grade grade;
	
	@RequestMapping("/goSaveStudent.html")
	public String goSaveStudent(HttpSession session){
//		List<Subject> subjectList = subjectService.getSubjectList();
//		session.setAttribute("subjectList",subjectList);
		return "counselor/addstudent";
	}
	
	@RequestMapping("/saveStudent.html")
	public String saveStudent(HttpSession session,Student student,String content){
		int num = 0;
		int gradeId = student.getGradeId();
		student.setState(1);
		student.setRegisterTime(new Date());
		User user = (User)session.getAttribute("user");
		
		
		notice.setAddresserId(user.getId());
		notice.setRecipientsId(userService.findUserByGradeId(gradeId));
		notice.setContent(content);
		notice.setNoticeDate(new Date());
//		notice.setStudentId(student.getId());
		num = noticeService.sendNotice(notice);
		num = studentService.SaveStudent(student);
		if(num > 0){
			return "redirect:/sys/student/studentList.html";
		}
		
		return "counselor/StuList";
	}
		
	@RequestMapping("/studentList.html")
	public String studentList(HttpSession session){
		List<Student> studentList = studentService.findStudentByCounselor();
		session.setAttribute("studentList", studentList);
		return "counselor/StuList";
	}
	
	@RequestMapping(value="/findStudnetById.json",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	@ResponseBody
	public Object findStudentById(Integer id,HttpSession session,HttpServletRequest request){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		Student student = studentService.findStudentById(id);
		String birthdayDome = student.getBirthday().toLocaleString().substring(0,8);
		
		resultMap.put("student", student);
		resultMap.put("birthday",birthdayDome);
		
		return JSONArray.toJSONString(resultMap);
	}
	
	@RequestMapping(value="/updatestudent.html",method=RequestMethod.GET)
	public String goUpdateStudent(HttpSession session,Integer studentId){
		
		Student student = studentService.findStudentById(studentId);
		List<Grade> gradeList = gradeService.getGradeBySubjectId(student.getGrade().getSubject().getId());
		session.setAttribute("student", student);
		session.setAttribute("gradeList", gradeList);
//		String birthday = student.getBirthday().toLocaleString().substring(0,8);
//		session.setAttribute("updateBirthday", birthday);
		return "counselor/updatestudent";
	}
	
	@RequestMapping(value="/updatestudent.html",method=RequestMethod.POST)
	public String updateStudent(Student student){
		int num = studentService.updateStudent(student);
		if(num > 0){
			System.out.println("ok");
			return "redirect:/sys/student/studentList.html";
		}
		return "counselor/StuList";
	}
	
	/**
	 * 10-19
	 * 根据班主任所任班级Id查询该班级下的所有学生
	 * */
	@RequestMapping("/studentListByGradeId.html")
	public String studentListByGradeId(Integer gradeId,HttpSession session){
		
		List<Student> studentList = studentService.findStudentListByGradeId(gradeId);
		session.setAttribute("studentList",studentList);
		
		return "formteacher/sminylist";
	}
	
	
	
	
}
